package com.xoa.service.party;


import com.xoa.dao.party.MapOwnerinfoMapper;
import com.xoa.dao.party.TrashMapper;
import com.xoa.model.party.Trash;
import com.xoa.util.ToJson;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.beans.Transient;
import java.io.File;
import java.io.InputStream;
import java.util.*;

@Service
public class TrashService {
    @Autowired
    private TrashMapper trashMapper;
    @Autowired
    private MapOwnerinfoMapper mapOwnerinfoMapper;

    /**
     * 查询
     * @param page
     * @param pageSize
     * @param tid
     * @return
     */
    public ToJson selectTrash(Integer page, Integer pageSize, Integer tid, boolean useFlag, String codeNo){
            ToJson json=new ToJson();
            Map <String,Object> map=new HashMap<String,Object>();
            page=(page-1)*pageSize;
            map.put("page",page);
            map.put("pageSize",pageSize);
            map.put("tid",tid);
            map.put("codeNo",codeNo);
            try {
                List<Trash> list=trashMapper.selectTrash(map);
                int taol=trashMapper.count(map);
                    json.setObject(taol);
                    json.setFlag(0);
                    json.setObj(list);
                    json.setMsg("ok");
                    return json;
            }catch (Exception e){
                e.printStackTrace();
            }
            return json;
    }

    /**
     * 删除
     * @param tid
     * @return
     */
    @Transient
    public ToJson delete(Integer tid){
            ToJson json=new ToJson();
            try{
                int count=trashMapper.deleteByPrimaryKey(tid);
                if(count>0){
                    json.setMsg("ok");
                    json.setFlag(0);
                    return json;
                }
                json.setMsg("删除失败");
                json.setFlag(0);
            }catch (Exception e){
                e.printStackTrace();
            }
            return json;
    }

    /**
     * 修改
     * @param trash
     * @return
     */
    @Transient
    public ToJson update(Trash trash){
        ToJson json=new ToJson();
        try{
            int count=trashMapper.updateByPrimaryKey(trash);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
                return json;
            }
            json.setMsg("修改失败");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 添加
     */
    @Transient
    public ToJson add(Trash trash){
        ToJson json=new ToJson();
        try{
            int count=trashMapper.insert(trash);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
                return json;
            }
            json.setMsg("添加失败");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }


    public ToJson ecoudInsert(MultipartFile file){
        int count=0;//計數器
        ToJson json=new ToJson();
        try{
            if (file.isEmpty()) {
                json.setMsg("请上传文件！");
                json.setFlag(1);
                return json;
            }else {
                String fileName = file.getOriginalFilename();
                if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    int pos = fileName.indexOf(".");//判读当前系统
                    //读取配置文件
                    ResourceBundle rb = ResourceBundle.getBundle("upload");
                    String osName = System.getProperty("os.name");
                    StringBuffer path = new StringBuffer();
                    if (osName.toLowerCase().startsWith("win")) {
                        path = path.append(rb.getString("upload.win"));
                    } else {
                        path = path.append(rb.getString("upload.linux"));
                    }
                    String extName = fileName.substring(pos);
                    String newFileName = uuid + extName;
                    File dest = new File(path.toString(), newFileName);
//                    file.transferTo(dest);
//                    // 将文件上传成功后进行读取文件
//                    // 进行读取的路径
//                    String readPath = path.append(newFileName).toString();
//                    InputStream in = new FileInputStream(readPath);
                    InputStream in =  file.getInputStream();

                    HSSFWorkbook excelObj = new HSSFWorkbook(in);
                    HSSFSheet sheetObj = excelObj.getSheetAt(0);
                    Row row = sheetObj.getRow(0);
                    int colNum = row.getPhysicalNumberOfCells();
                    int lastRowNum = sheetObj.getLastRowNum();
                    Trash trash = null;
                    for (int i = 1; i <= lastRowNum; i++) {
                        trash = new Trash();
                        row = sheetObj.getRow(i);
                        String value = "";
                        if (row != null) {
                            for (int j = 0; j < colNum; j++) {
                                Cell cell = row.getCell(j);
                                if (cell != null) {
                                    switch (j) {
                                        case 0:
                                            //所属社区
                                            if(cell.getCellType() == CellType.NUMERIC){
                                                cell.setCellType(CellType.STRING);
                                            }
                                            trash.setDeptid(Integer.valueOf(cell.getStringCellValue()));
                                            break;
                                        case 1:
                                            //垃圾桶总数
                                            if(cell.getCellType() == CellType.NUMERIC){
                                                cell.setCellType(CellType.STRING);
                                            }
                                            trash.setTratcount(Integer.valueOf(cell.getStringCellValue()==null?"0":cell.getStringCellValue()));
                                            break;
                                        case 2:
                                            //架子总数
                                            if(cell.getCellType() == CellType.NUMERIC){
                                                cell.setCellType(CellType.STRING);
                                            }
                                            trash.setShelfcount(Integer.valueOf(cell.getStringCellValue()==null?"0":cell.getStringCellValue()));
                                            break;
                                        case 3:
                                            //绿桶数量
                                            if(cell.getCellType() == CellType.NUMERIC){
                                                cell.setCellType(CellType.STRING);
                                            }
                                            trash.setGreenbarrelcount(Integer.valueOf(cell.getStringCellValue()==null?"0":cell.getStringCellValue()));
                                            break;
                                        case 4:
                                            //灰桶数量
                                            if(cell.getCellType() == CellType.NUMERIC){
                                                cell.setCellType(CellType.STRING);
                                            }
                                            trash.setAshbucketcount(Integer.valueOf(cell.getStringCellValue()==null?"0":cell.getStringCellValue()));
                                            break;
                                        case 5:
                                            //破损
                                            if(cell.getCellType() == CellType.NUMERIC){
                                                cell.setCellType(CellType.STRING);
                                            }
                                            trash.setBedamaged(Integer.valueOf(cell.getStringCellValue()==null?"0":cell.getStringCellValue()));
                                            break;
                                        case 6:
                                            //备注
                                            if(cell.getCellType() == CellType.NUMERIC){
                                                cell.setCellType(CellType.STRING);
                                            }
                                            trash.setRemaks(cell.getStringCellValue()==null?"":cell.getStringCellValue());
                                            break;
                                        case 7:
                                            trash.setGridLocation(mapOwnerinfoMapper.selectCoordingbyName(cell.getStringCellValue()));
                                            break;
                                        default:
                                            json.setFlag(0);
                                            json.setMsg("err");
                                            break;
                                    }
                                }

                            }
                            //将数据放入数据库
                            int count1=trashMapper.insertSelective(trash);
                            if(count1>0) {
                                count += count1;
                            }
                            dest.delete();
                        }
                    }

                }
            }
            if(count>0){
                json.setTotleNum(count);
                json.setMsg("ok");
                json.setFlag(0);
                return  json;
            }
        }catch (Exception e){
            json.setMsg("err");
            e.printStackTrace();
        }finally {
            return json;
        }
    }
}
